//
//  CIBNVIPController.m
//  CIBN
//
//  Created by ZNGOO on 16/7/18.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNVIPController.h"
#import "CIBNHomePageScrollCell.h"
#import "CIBHomeListCell.h"
#import "CIBNBlockListModel.h"
#import "CIBNHomeListModel.h"
#import "CIBNPlayController.h"

static NSString *ListCell = @"CIBHomeListCell";
static NSString *PageScrollCell = @"CIBNHomePageScrollCell";
@interface CIBNVIPController () <UICollectionViewDelegate, UICollectionViewDataSource>


@property (nonatomic,strong) UICollectionView * collectionView;

/**  分类数据 */
@property (nonatomic,strong) NSMutableArray * blockArray;

/**  主页数据 */
@property (nonatomic,strong) NSMutableArray * homeArray;

@end

@implementation CIBNVIPController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setItem];
    
    [self createCollectionView];
    
    [self loadData];
}


- (void)createCollectionView{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:layout];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[CIBNHomePageScrollCell class] forCellWithReuseIdentifier:PageScrollCell];
    [self.collectionView registerClass:[CIBHomeListCell class] forCellWithReuseIdentifier:ListCell];
    [self.view addSubview:self.collectionView];
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    switch (section) {
        case 0:
        {
            return UIEdgeInsetsMake(0, 0, 10, 0);
        }
            break;
            
        default:{
            
            return UIEdgeInsetsMake(0, 5, 10, 5);
        }
            break;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            return CGSizeMake(ScreenWidth, ScreenWidth * 9 / 16);
        }
            break;
            
        default:{

            return CGSizeMake((ScreenWidth - 40)/3, (((ScreenWidth - 40) /3) * 16/9) + 20);
        }
            break;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.blockArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (section) {
        case 0:
        {
            return 1;
        }
            break;
            
        default:
        {
            return [self loadSectionNum:section];
        }
            break;
    }
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            CIBNHomePageScrollCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:PageScrollCell forIndexPath:indexPath];
            [cell setScrollViewImageArr:[self loadScrollViewArr:indexPath.section]];
            
            cell.pushPlayVCBlock = ^(CIBNHomeListModel * model){
                CIBNPlayController * playVC = [[CIBNPlayController alloc] init];
                playVC.model = model;
                [self.navigationController pushViewController:playVC animated:YES];
            };
            return cell;
        }
            break;
            
        default:
        {
            CIBHomeListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ListCell forIndexPath:indexPath];
            CIBNHomeListModel * model = [self loadScrollViewArr:indexPath.section][indexPath.row];
            [cell setHomeCell:model recomend:0];
            return cell;
        }
            break;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CIBNHomeListModel * model = [self loadScrollViewArr:indexPath.section][indexPath.row];
    CIBNPlayController * playVC = [[CIBNPlayController alloc] init];
    playVC.model = model;
    [self.navigationController pushViewController:playVC animated:YES];
    
}
- (void)setItem{
    self.leftBtn.hidden = YES;
    self.rightBtn2.hidden = YES;
    [self.rightBtn1 setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    self.centerTitle.text = @"会员专区";
}


- (void)loadData{
    
    [RJNetwoking POST:Domain_URL body:@{
                                        @"Cmdid":@"4672",
                                        @"Seqid":@"22",
                                        @"ProtoVer":@"1",
                                        @"UID":@"3000000001",
                                        @"HID":@"56D7A9EA3A70EC3618391BA1A3CB59EE",
                                        @"TID":@"52490",
                                        @"CliVer":@"72058693549555712",
                                        @"Oemid":@"10001",
                                        @"Epgid":@"9",
                                        @"Sessid":@"14850116173381489434",
                                        @"TermType":@"30",
                                        @"SubjectID":@"1551"
                                        }
              success:^(id  _Null_unspecified responseObject) {
                  
                  self.blockArray = [CIBNBlockListModel mj_objectArrayWithKeyValuesArray:responseObject[@"BlockList"]];
                  self.homeArray = [CIBNHomeListModel mj_objectArrayWithKeyValuesArray:responseObject[@"MenuList"]];
                  
                  
                  for (CIBNBlockListModel * model in self.blockArray) {
                      NSLog(@"%@",model.Name);
                  }
                  
                  
                  [self.collectionView reloadData];
                  
              } failure:^(NSError * _Nonnull error) {
                  
                  [MBProgressHUD bwm_showTitle:@"出错啦" toView:self.view hideAfter:2.0 msgType:BWMMBProgressHUDMsgTypeError];
              }];
}

#pragma mark - 计算每组个数
- (NSUInteger)loadSectionNum:(NSUInteger)section{
    
    NSUInteger num = 0;
    for (int index = 0; index < self.homeArray.count; index++) {
        CIBNBlockListModel * model = self.blockArray[section];
        CIBNHomeListModel * homeModel = self.homeArray[index];
        
        if ([homeModel.blockid isEqualToString:model.BlockID]) {
            num++;
        }
    }
    
    if (num > 6) {
        return 6;
    }else{
        return num;
    }
}

#pragma mark - 计算每组的元素
- (NSMutableArray *)loadScrollViewArr:(NSUInteger)section{
    
    NSMutableArray * array = [[NSMutableArray alloc] init];
    for (int index = 0; index < self.homeArray.count; index++) {
        CIBNBlockListModel * model = self.blockArray[section];
        CIBNHomeListModel * homeModel = self.homeArray[index];
        
        if ([homeModel.blockid isEqualToString:model.BlockID]) {
            [array addObject:homeModel];
        }
    }
    
    return array;
}

-(void)viewWillAppear:(BOOL)animated{
    [InterfacOrientation setInterfacOrientation:UIInterfaceOrientationPortrait];
    self.tabBarController.tabBar.hidden = NO;
}
@end
